<!DOCTYPE html>
<html lang="zh" xmlns:th="http://www.thymeleaf.org" xmlns:layout="http://www.ultraq.net.nz/thymeleaf/layout"
      layout:decorate="~{layout/default}">
<head>
    <title>用户管理</title>
</head>
<body>
    <div layout:fragment="content" class="fade-in">
        <div class="d-flex justify-content-between align-items-center mb-4">
            <h2><i class="fas fa-users me-2"></i>用户管理</h2>
            <a href="/admin" class="btn btn-secondary">
                <i class="fas fa-arrow-left me-2"></i>返回控制台
            </a>
        </div>
        
        <div th:if="${successMessage}" class="alert alert-success alert-dismissible fade show" role="alert">
            <span th:text="${successMessage}"></span>
            <button type="button" class="btn-close" data-bs-dismiss="alert" aria-label="Close"></button>
        </div>
        
        <div class="card">
            <div class="card-body">
                <div class="table-responsive">
                    <table class="table table-striped table-hover">
                        <thead>
                            <tr>
                                <th>ID</th>
                                <th>用户名</th>
                                <th>邮箱</th>
                                <th>角色</th>
                                <th>注册时间</th>
                                <th>操作</th>
                            </tr>
                        </thead>
                        <tbody>
                            <tr th:each="user : ${users}">
                                <td th:text="${user.id}"></td>
                                <td th:text="${user.username}"></td>
                                <td th:text="${user.email}"></td>
                                <td>
                                    <span th:if="${user.role.name() == 'ADMIN'}" class="badge bg-danger">管理员</span>
                                    <span th:if="${user.role.name() == 'USER'}" class="badge bg-primary">用户</span>
                                </td>
                                <td th:text="${#temporals.format(user.createdAt, 'yyyy-MM-dd HH:mm')}"></td>
                                <td>
                                    <div class="btn-group">
                                        <button type="button" class="btn btn-sm btn-outline-primary dropdown-toggle" data-bs-toggle="dropdown">
                                            操作
                                        </button>
                                        <ul class="dropdown-menu">
                                            <li>
                                                <a th:href="@{/admin/users/{id}(id=${user.id})}" class="dropdown-item">
                                                    <i class="fas fa-eye me-2"></i>查看详情
                                                </a>
                                            </li>
                                            <li>
                                                <a th:href="@{/admin/users/{id}/edit(id=${user.id})}" class="dropdown-item">
                                                    <i class="fas fa-user-edit me-2"></i>编辑用户
                                                </a>
                                            </li>
                                            <li th:if="${user.role.name() == 'USER'}">
                                                <form th:action="@{/admin/users/{id}/role(id=${user.id})}" method="post" class="d-inline">
                                                    <input type="hidden" name="role" value="ADMIN">
                                                    <button type="submit" class="dropdown-item">
                                                        <i class="fas fa-user-shield me-2"></i>设为管理员
                                                    </button>
                                                </form>
                                            </li>
                                            <li th:if="${user.role.name() == 'ADMIN' && user.username != 'admin'}">
                                                <form th:action="@{/admin/users/{id}/role(id=${user.id})}" method="post" class="d-inline">
                                                    <input type="hidden" name="role" value="USER">
                                                    <button type="submit" class="dropdown-item">
                                                        <i class="fas fa-user me-2"></i>设为普通用户
                                                    </button>
                                                </form>
                                            </li>
                                            <li th:if="${user.username != 'admin'}">
                                                <hr class="dropdown-divider">
                                                <form th:action="@{/admin/users/{id}/delete(id=${user.id})}" method="post" class="d-inline"
                                                      onsubmit="return confirm('确定要删除该用户吗？此操作不可撤销！')">
                                                    <button type="submit" class="dropdown-item text-danger">
                                                        <i class="fas fa-trash-alt me-2"></i>删除用户
                                                    </button>
                                                </form>
                                            </li>
                                        </ul>
                                    </div>
                                </td>
                            </tr>
                        </tbody>
                    </table>
                </div>
            </div>
        </div>
    </div>
</body>
</html> 